package Mybatis;

import Mybatis.controller.UserController;
import Mybatis.dao.IUserDao;
import Mybatis.model.User;
import Mybatis.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MybatisTest {
    private IUserDao userDao;

    @Before
    public void before() throws IOException {
        InputStream resource = Resources.getResourceAsStream("sqlMapperConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        userDao = sqlSession.getMapper(IUserDao.class);
    }

    @Test
    public void test(){
        System.out.println("hello spring");
    }

    @Test
    public void testFindAll(){
        List<User> userList = userDao.findAll();
        for (User user:userList){
            System.out.println(user.toString());
        }
    }

    private User createUser(){
        User user = new User(3,"wangwu","男","瓯江口",new Date(2021, 6, 12));
        return user;
    }

    @Test
    public void testInsert(){
        User user = createUser();
        int res = userDao.save(user);
        System.out.println(res);
    }

    @Test
    public void testUpdate(){
        int res = userDao.update("wangliu", "瓯海", 3);
        System.out.println(res);
    }

    @Test
    public void testDelete(){
        int res = userDao.delete(3);
        System.out.println(res);
    }

    @Test
    public void testMybatisBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springContext.xml");
        IUserDao userDao = (IUserDao) applicationContext.getBean("userDao");
        List<User> userList = userDao.findAll();
        for (User user:userList){
            System.out.println(user.toString());
        }
    }

    @Test
    public void testMybatisService(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springContext.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        List<User> userList = userService.findAll();
        for (User user:userList){
            System.out.println(user.toString());
        }
    }

    @Test
    public void testMybatisController(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springContext.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        String s = userController.helloSpringMVC();
        System.out.println(s);
    }
}
